﻿/*文本框上传图片*/

var textUploadModule = angular.module('textUploadModule', ['knowledgebaseS', 'Common']);
textUploadModule.directive('textupload1', ["$rootScope", "$timeout", "$parse", "Msg", 'sysUserInfo', 'RequestTrainService', function ($rootScope, $timeout, $parse, Msg, sysUserInfo, RTS) {
    return {
        restrict: "AECM",
        //replace: true,       
        templateUrl: "/system/directive/drop-down/text-upload.html",
        scope: {
            mymodel: '=',
            mytxt: '=',
            uploader: '='
        },
        link: function (scope, element, attr) {

            //上传封面单击
            scope.uploadImg = function () {
                if (scope.upImgLoading) {
                    Msg.warning("有文件正在上传。。");
                    return;
                }
                document.getElementById("imgupload").click();
            }
            scope.upImgLoading = false;
            /*********************************************文件上传开始**********************************************/
            var takUpFile = scope.uploader //= new FileUploader();
            takUpFile.onWhenAddingFileFailed = function (item /*{File|FileLikeObject}*/, filter, options) {     // 选择文件后失败       0
                console.info('选择文件后失败', item, filter, options);
            };
            takUpFile.onAfterAddingFile = function (fileItem) {  // 选择文件后 （成功）   0   
                console.info('选择文件后', fileItem);
                // 上传判断是不是有文件 
                if (takUpFile.isUploading) {
                    Msg.warning("有文件正在上传。。");
                    return;
                } else {
                    scope.upImgLoading = true;
                    var upfileparam = "?fid=0&fpath=/&orgid=" + sysUserInfo.getuserinfo().organization_ID + "&userid=" + sysUserInfo.getuserinfo().user_ID + "&username=" + sysUserInfo.getuserinfo().username;
                    //上传文件路径
                    fileItem.url = RTS.uploadCover(upfileparam);
                }
            };
            takUpFile.onAfterAddingAll = function (addedFileItems) { // 选择文件后添加到队列   1 
                console.info('选择文件后添加到队列', addedFileItems);
                // 上传判断是不是有文件                        
                if (takUpFile.isUploading) {
                    Msg.warning("有文件正在上传。。");
                    return;
                } else {
                    takUpFile.uploadAll();
                }
            };
            // 开始上传
            takUpFile.onBeforeUploadItem = function (item) {    // 2 上传之前一项
                console.info('上传之前一项', item);
            };
            takUpFile.onProgressItem = function (fileItem, progress) {   // 3 上传进度
                console.info('上传进度', fileItem, progress);
            };
            takUpFile.onProgressAll = function (progress) {      //4 队列进度
                console.info('队列进度', progress);
            };
            //fileItem 请求信息 response: 返回值status: 返回状态  headers: 请求头 
            takUpFile.onSuccessItem = function (fileItem, response, status, headers) {   // 5上传成功
                console.info('5上传成功', fileItem, response, status, headers);
                scope.upImgLoading = false;
                if (response.errorcode == 0) {
                    //上传的模板
                    var block = "<label class='annex' >" + response.errormsg + "</label>"
                    scope.mymodel += block;
                    scope.mytxt = scope.mymodel;
                    console.log(scope.mymodel);
                    console.log(scope.mytxt);
                }
            };
            takUpFile.onErrorItem = function (fileItem, response, status, headers) {     // 上传地址有问题
                console.info('上传地址有问题', fileItem, response, status, headers);
                Msg.error("连接不上服务器,请刷新重试！");
                takUpFile.clearQueue();
            };
            takUpFile.onCompleteItem = function (fileItem, response, status, headers) {  //6     单文件上传完成
                console.info('单文件上传完成', fileItem, response, status, headers);
                takUpFile.clearQueue();
            };
            takUpFile.onCompleteAll = function () {  // 6 单文件上传完成或者队列上传完成的时候
                console.info('单文件上传完成或者队列上传完成的时候');
                takUpFile.clearQueue();  // 上传完毕清除队列
            };
            /*********************************************文件上传结束**********************************************/

        }
    }
} ]);


